package scheduler;

import java.util.Hashtable;

import property.Property;
import utility.CapitalChar;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Scheduler 
{
	DBManager database;
	GetIdName gid;
	String SCHEDULER="scheduler";
	String TYPE="type";	
	String SCHEDULE="schedule";
	String CLASS="class";
	String EVENT="event";
	
	public void getResult()
	{
		Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
		Condition.put("pid", TYPE);
		Condition.put("pv", SCHEDULER);
		RecordSearch rs =new RecordSearch(database);
		rs.setConditionMap(Condition);
		String line[] = rs.getArrayFromResultSet();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			Condition.clear();
			Condition.put("mid", splitline[Column.mid_index-1]);
			Condition.put("pid", SCHEDULE);
			rs.setConditionMap(Condition);
			String result[] = rs.getArrayFromResultSet();
			for(int j=0;j<result.length;j++)
			{
				String splitresult[] = result[i].split("\t");
				Condition.clear();
				Condition.put("mid", splitresult[Column.mid_index-1]);
				rs.setConditionMap(Condition);
				String result1[] = rs.getArrayFromResultSet();
				Hashtable<Object,Object> table = new Hashtable<Object, Object>();
				for(int k=0;k<result1.length;k++)
				{
					String splitresult1[] = result1[k].split("\t");
					String value= splitresult1[Column.pv_index-1];
					if(value.equals("0"))
						value = splitresult1[Column.vt_index-1];
					else
						value = gid.getItem(value); 
					table.put(gid.getItem(splitresult1[Column.pid_index-1]), value);
				}
				String Schedulobject = table.get("schedule").toString();
				Hashtable<Object, Object> SchduleMap = new Hashtable<Object, Object>();
				Condition.put("mid", gid.getId(Schedulobject));
				rs.setConditionMap(Condition);
				result1 = rs.getArrayFromResultSet();
				Condition.clear();
				for(int k=0;k<result1.length;k++)
				{
					String splitresult1[] = result1[k].split("\t");
					String value= splitresult1[Column.pv_index-1];
					if(value.equals("0"))
						value = splitresult1[Column.vt_index-1];
					else
						value = gid.getItem(value); 
					SchduleMap.put(gid.getItem(splitresult1[Column.pid_index-1]).toLowerCase() , value);
				}
				UpdateSchedule updateschedule = new UpdateSchedule();
				updateschedule.setData(database,SchduleMap, gid.getId(Schedulobject));
				boolean flag=updateschedule.getResult();
				String classobject = gid.getId(table.get(CLASS).toString());
				Property property = new Property(database);
				property.setMid(classobject);
				property.setPid(TYPE);			
				if(flag)
				{
					try 
					{
						scheduler.classes.Scheduler scheduler;	
						String classname = property.getValue();
						Class C = Class.forName("scheduler.classes."+CapitalChar.makeFirstCharCapital(classname));
						scheduler =(scheduler.classes.Scheduler)C.newInstance();
						scheduler.getData(database, classobject);
						scheduler.getResult();
					}
					catch (ClassNotFoundException e) 
					{
						e.printStackTrace();
					} catch (InstantiationException e) 
					{
						e.printStackTrace();
					}
					catch (IllegalAccessException e) 
					{
						e.printStackTrace();
					}
				}
			}
		}
		
	}
	
	public void SetDBManager(DBManager database)
	{
		this.database = database;
		gid= new GetIdName(database);
		SCHEDULER = gid.getId(SCHEDULER);
		TYPE =  gid.getId(TYPE);
		SCHEDULE = gid.getId(SCHEDULE);
		EVENT = gid.getId(EVENT);
	}
	public static void main(String[] args) 
	{
		DBManager database =new DBManager();
		database.getConnect();
		Scheduler sc =new Scheduler();
		sc.SetDBManager(database);
		sc.getResult();
	}
}
